



export default {
  debounce(fn,delay){
  let timer = null;
  return function(...args){
    //if(timer){clearTimeout(timer)}
    clearTimeout(timer);
    timer = setTimeout(()=>{
      // timer = null;
      fn.apply(this,args);
    },delay)
  }
},
  getCurrentDate () {
    let now = new Date()
    let year = now.getFullYear()
    let month = now.getMonth() + 1
    let day = now.getDate()
    month = month < 10 ? '0' + month : month
    day = day < 10 ? '0' + day : day
    return year + '-' + month + '-' + day
  },

  getCurrentMonth () {
    let now = new Date()
    let year = now.getFullYear()
    let month = now.getMonth() + 1
    month = month < 10 ? '0' + month : month
    return year + '-' + month
  },
  download(data, titName){
    if (!data) {
      return
    }
    const content = data
    const blob = new Blob([content],  {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'})
    const fileName = titName ? titName : ''
    if ('download' in document.createElement('a')) { // 非IE下载
      const elink = document.createElement('a')
      elink.download = fileName
      elink.style.display = 'none'
      elink.href = URL.createObjectURL(blob)
      document.body.appendChild(elink)
      elink.click()
      URL.revokeObjectURL(elink.href) // 释放URL 对象
      document.body.removeChild(elink)
    } else { // IE10+下载
      navigator.msSaveBlob(blob, fileName)
    }
  },
  // 根据类型获取数组中对应的名称
  getTypeName (type, arr) {
    const filters = arr.filter(item => {
      return item.value == type
    })
    if (filters.length > 0) {
      return filters[0].label
    }
  },
  // 拷贝数组
  copyArr (arr) {
    const strArr = JSON.stringify(arr)
    return JSON.parse(strArr)
  },
  getUrlKey (name, url) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(url) || [, ''])[1].replace(/\+/g, '%20')) || null
  },
  fileName (name) {
    let date = new Date()
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let fileUrl = process.env.NODE_ENV == 'production' ? 'safe-edu-prd/' : 'safe-edu-test/'
    // 保存路径
    var _fileName = fileUrl + year + '-' + month + '/' + Date.parse(new Date()) +
      parseInt(Math.random() * (100000 - 10000 + 1) + 10000, 10) + '.' + name
    return _fileName
  },
  // 获取视频时长
  getMp4Time (file) {
    return new Promise(async (resolve, reject) => {
      let url = URL.createObjectURL(file)
      let audioElement = new Audio(url)
      let durtaion = 0
      // 下面需要注意的是在监听loadedmetadata绑定的事件中对duration直接进行赋值是无效的，需要在fun回调函数中进行赋值
      audioElement.addEventListener('loadedmetadata', function () {
        // 音频/视频的元数据已加载时，会发生 loadedmetadata 事件
        durtaion = audioElement.duration.toFixed(0) // 时长以秒作为单位
        fun(parseFloat(durtaion))
      })
      let fun = (s) => {
        durtaion = s
        resolve(durtaion)
      }
    })
  },
  convertNumToStr (value) {
    const sum = 5
    const currLen = value.toString().length
    let num = 0
    let str = ''
    let arr = []
    if (sum > currLen) {
      num = sum - currLen
    }
    for (let i = 0; i < num; i++) {
      str += '0,'
    }
    str += value
    arr = str.split(',')
    return arr
  },
  // 获取文件大小
  getFileSize (size) {
    let units = ['B', 'KB', 'MB', 'GB', 'TB']
    let i = 0
    while (size >= 1024 && i < units.length - 1) {
      size /= 1024
      i++
    }
    return size.toFixed(2) + ' ' + units[i]
  }
}
